package com.lq.rocketmq.batch;

import com.lq.rocketmq.config.MyConstant;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.zip.Inflater;

/**
 * @program: myrocketmq
 * @description: 发送批量消息
 * @author: LQ
 * @create: 2021-05-07 17:20
 */

public class BatchProducer {
    public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
        //1.创建消息生产者producer,并制定生产者组名
        DefaultMQProducer producer = new DefaultMQProducer(MyConstant.MSG_GROUP);
        //2.指定Nameserver地址
        producer.setNamesrvAddr(MyConstant.NAMESRV_ADDR);
        //producer.setNamesrvAddr("192.168.233.250:9876;192.168.233.251:9876 ");
        //3.启动producer
        producer.start();
        //4.构建消息集合
        List<Message> msgList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            msgList.add(new Message(MyConstant.TOPIC_BATCH, MyConstant.TAG1, (("Hello world") + i).getBytes()));
        }
        producer.send(msgList);
        //6.关闭生产者producer
        producer.shutdown();
    }

}
